function MyEmote_ToggleDropDown(this)
	MyEmote_DropDown.point = "BOTTOMLEFT";
	MyEmote_DropDown.relativePoint = "BOTTOMRIGHT";
	ToggleDropDownMenu(1, nil, MyEmote_DropDown);
end

function MyEmote_DropDown_OnLoad(this)
	UIDropDownMenu_Initialize(this, MyEmote_DropDown_InitButtons, "MENU");
end

function MyEmote_DropDown_InitButtons(self, level)
	level = level or 1
	local info = {};

	if level == 1 then
		for key, value in pairs(EL_Types) do
			local info = UIDropDownMenu_CreateInfo();
			info.text = value;
			info.value = key;
			info.hasArrow = 1;
			info.notCheckable = 1;
			UIDropDownMenu_AddButton(info, 1);
		end

	elseif level == 2 then
		local hasTarget = UnitName("target");
		if (hasTarget == "Unknown") then
			hasTarget = false;
		end

		for key, value in pairs(EL_Emotes) do
			for key2, value2 in pairs(value.types) do
				if (UIDROPDOWNMENU_MENU_VALUE == value2) then
					local info = UIDropDownMenu_CreateInfo();
					if (hasTarget) then
						info.text = MyEmote_GetOnDemandText(value,true);
						info.text = string.gsub(info.text,"<Target>",hasTarget);
					else
						info.text = MyEmote_GetOnDemandText(value,false);
					end

					if (EL_Types[value["types"][1]] and value["types"][1] == 3) then	-- Custom emote
						local genderCode = UnitSex("player");
						local genderHe = nil;
						local genderHis = nil;
						local genderhe = nil;
						local genderhis = nil;
						if EL_Gender then
							genderHe = EL_Gender[genderCode]["He"];
							genderHis = EL_Gender[genderCode]["His"];
							genderhe = EL_Gender[genderCode]["he"];
							genderhis = EL_Gender[genderCode]["his"];
						else
							if (genderCode==2) then -- male
								genderHe = "He";
								genderhe = "he";
								genderHis = "His";
								genderhis = "his";
							else -- female (we hope)
								genderHe = "She";
								genderhe = "she";
								genderHis = "Her";
								genderhis = "her";
							end
						end
						info.text = string.gsub(info.text,"<He>",genderHe);
						info.text = string.gsub(info.text,"<His>",genderHis);
						info.text = string.gsub(info.text,"<he>",genderhe);
						info.text = string.gsub(info.text,"<his>",genderhis);
					end

					info.value = key;
					info.func = MyEmote_HandleModClick;
					info.notCheckable = 1;
					UIDropDownMenu_AddButton(info, 2);
				end
			end
		end
	end
end

function MyEmote_GetOnDemandText(value,hasTarget)
	local color;
	local flag = nil;
	local returnCode;
	local emoteText;

	if (hasTarget) then emoteText = value.target else emoteText = value.none end

	if (EL_Types[value["types"][1]] and value["types"][1] == 3) then
		emoteText = UnitName("player").." "..emoteText;	-- Custom emote
	end

	if value.react == 0 then		-- None (text only) White
		color = "fffefefe";
	elseif value.react == 1 then		-- Animated - Purple
		color = "ffa335ee";
		flag = EL_React[value.react];
	elseif value.react == 2 then		-- Voice - Orange
		color = "ffff8000";
		flag = EL_React[value.react];
	elseif value.react == 3 then		-- Both - Green
		color = "ff1eff00";
		flag = EL_React[value.react];
	else					-- Grey (Unknown)
		color = "ff9d9d9d";
	end

	returnCode = "|c" .. color .. emoteText .. FONT_COLOR_CODE_CLOSE;
	if (flag) then returnCode = returnCode.." ["..flag.."]"; end
	return returnCode;
end

function MyEmote_HandleModClick()
	if (EL_Types[EL_Emotes[this.value]["types"][1]] and EL_Emotes[this.value]["types"][1] == 3) then	-- Custom emote
		local emoteText;

		local hasTarget = UnitName("target");
		if (hasTarget and hasTarget ~= "Unknown") then
			emoteText = EL_Emotes[this.value].target;
			emoteText = string.gsub(emoteText,"<Target>",hasTarget);
		else
			emoteText = EL_Emotes[this.value].none;
		end

		local genderCode = UnitSex("player");
		local genderHe = nil;
		local genderHis = nil;
		local genderhe = nil;
		local genderhis = nil;
		if EL_Gender then
			genderHe = EL_Gender[genderCode]["He"];
			genderHis = EL_Gender[genderCode]["His"];
			genderhe = EL_Gender[genderCode]["he"];
			genderhis = EL_Gender[genderCode]["his"];
		else
			if (genderCode==2) then -- male
				genderHe = "He";
				genderhe = "he";
				genderHis = "His";
				genderhis = "his";
			else -- female (we hope)
				genderHe = "She";
				genderhe = "she";
				genderHis = "Her";
				genderhis = "her";
			end
		end
		emoteText = string.gsub(emoteText,"<He>",genderHe);
		emoteText = string.gsub(emoteText,"<His>",genderHis);
		emoteText = string.gsub(emoteText,"<he>",genderhe);
		emoteText = string.gsub(emoteText,"<his>",genderhis);

		SendChatMessage(emoteText,"EMOTE");
	else
		emoteToken = string.upper(this.value);

		if (emoteToken == "LAVISH") then
			emoteToken = "PRAISE";
		end
		if (emoteToken == "EXCITED") then
			emoteToken = "TALKEX";
		end
		if (emoteToken == "DOOM") then
			emoteToken = "THREATEN";
		end
		if (emoteToken == "SILLY") then
			emoteToken = "JOKE";
		end
		if (emoteToken == "LAY") then
			emoteToken = "LAYDOWN";
		end
		if (emoteToken == "REAR") then
			emoteToken = "SHAKE";
		end
		if (emoteToken == "BELCH") then
			emoteToken = "BURP";
		end
		if (emoteToken == "SMELL") then
			emoteToken = "STINK";
		end
		if (emoteToken == "GOODBYE") then
			emoteToken = "BYE";
		end
		if (emoteToken == "FOLLOWME") then
			emoteToken = "FOLLOW";
		end
		if (emoteToken == "ATTACKTARGET") then
			emoteToken = "ATTACKMYTARGET";
		end
		if (emoteToken == "CONGRATS") then
			emoteToken = "CONGRATULATE";
		end
		if (emoteToken == "PUZZLED") then
			emoteToken = "PUZZLE";
		end
		if (emoteToken == "QUESTION") then
			emoteToken = "TALKQ";
		end

		DoEmote(emoteToken);
	end
end
